﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace YJKRevitAxisModule
{
	public class AxisNameManager
	{
		public AxisNameManager(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public bool AddNewName(string name)
		{
			name = name.ToUpper();
			string text = "";
			int num = 0;
			int num2 = 0;
			_tagCharType tagCharType = AxisNameManager.CharType(name, ref text, ref num, ref num2);
			if (tagCharType == _tagCharType.CHARTYPE_OTHER)
			{
				return false;
			}
			this.AddOneNewAxisName(name, tagCharType);
			return true;
		}

		public bool ChangeName(string oldName, string newName)
		{
			oldName = oldName.ToUpper();
			newName = newName.ToUpper();
			string text = "";
			int num = 0;
			int num2 = 0;
			_tagCharType charType = AxisNameManager.CharType(oldName, ref text, ref num, ref num2);
			return this.ChangeName(oldName, newName, charType);
		}

		public bool RemoveName(string name)
		{
			name = name.ToUpper();
			string text = "";
			int num = 0;
			int num2 = 0;
			_tagCharType tagCharType = AxisNameManager.CharType(name, ref text, ref num, ref num2);
			return tagCharType != _tagCharType.CHARTYPE_OTHER && tagCharType != _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND && tagCharType != _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND && tagCharType != _tagCharType.CHARTYPE_NUMERAL_REVERSE_SECOND && tagCharType != _tagCharType.CHARTYPE_NUMERAL_SECOND && this.RemoveOneAxixName(name, tagCharType);
		}

		public bool MainAuxiliaryConversion(string name, ref string message)
		{
			name = name.ToUpper();
			string text = "";
			int mainGenericCode = 0;
			int secondNumber = 0;
			_tagCharType tagCharType = AxisNameManager.CharType(name, ref text, ref mainGenericCode, ref secondNumber);
			if (tagCharType == _tagCharType.CHARTYPE_OTHER)
			{
				message = "特殊符号轴线无法进行主辅转换";
				return false;
			}
			return this.MainAuxiliaryConversion(name, tagCharType, mainGenericCode, secondNumber, ref message);
		}

		public static string GetNextLable(string lable)
		{
			return AxisNameManager.GetNextLable(lable, 1);
		}

		public static string GetNextLable(string lable, int count)
		{
			string containsPrefix = "";
			string text = lable;
			if (!AxisNameManager.IsContainsPrefix(lable, ref containsPrefix, ref text))
			{
				text = lable;
			}
			string message = "";
			if (!AxisNameManager.IsValidatingOfName(text, false, ref message))
			{
				YJKMessageBox.Show(message);
				return "";
			}
			string text2 = "";
			text = text.ToUpper();
			int mainGenericCode = 0;
			int secondNumber = 0;
			_tagCharType tagCharType = AxisNameManager.CharType(text, ref text2, ref mainGenericCode, ref secondNumber);
			if (tagCharType == _tagCharType.CHARTYPE_OTHER)
			{
				return "";
			}
			GridInfos gridInfos = new GridInfos(tagCharType, containsPrefix, mainGenericCode, secondNumber, null);
			for (int i = 0; i < count; i++)
			{
				gridInfos = gridInfos.GetNextGridInfos();
			}
			return gridInfos.Name();
		}

		public static List<string> CalculatSortedName(string startName, int count)
		{
			List<string> list = new List<string>();
			string containsPrefix = "";
			int mainGenericCode = 0;
			int secondNumber = 0;
			_tagCharType tagCharType = AxisNameManager.CharType(startName, ref containsPrefix, ref mainGenericCode, ref secondNumber);
			if (tagCharType == _tagCharType.CHARTYPE_OTHER)
			{
				return list;
			}
			GridInfos item = new GridInfos(tagCharType, containsPrefix, mainGenericCode, secondNumber, null);
			List<GridInfos> list2 = new List<GridInfos>();
			list2.Add(item);
			for (int i = 0; i < count - 1; i++)
			{
				GridInfos nextGridInfos = list2[list2.Count - 1].GetNextGridInfos();
				list2.Add(nextGridInfos);
			}
			foreach (GridInfos gridInfos in list2)
			{
				list.Add(gridInfos.Name());
			}
			return list;
		}

		public static Element FindNameGetGrid(string name, Autodesk.Revit.DB.Document doc)
		{
			Element result = null;
			foreach (Element element in AxisNameManager.GetAllAxis(doc))
			{
				if (element.Name.CompareTo(name) == 0)
				{
					result = element;
					break;
				}
			}
			return result;
		}

		public static bool GridNameCompare(string name1, string name2, ref int res)
		{
			string text = "";
			int mainGenericCode = 0;
			int secondNumber = 0;
			_tagCharType tagCharType = AxisNameManager.CharType(name1, ref text, ref mainGenericCode, ref secondNumber);
			if (tagCharType == _tagCharType.CHARTYPE_OTHER)
			{
				return false;
			}
			string text2 = "";
			int mainGenericCode2 = 0;
			int secondNumber2 = 0;
			_tagCharType tagCharType2 = AxisNameManager.CharType(name2, ref text2, ref mainGenericCode2, ref secondNumber2);
			if (tagCharType2 == _tagCharType.CHARTYPE_OTHER)
			{
				return false;
			}
			if (text.CompareTo(text2) != 0)
			{
				return false;
			}
			GridInfos a = new GridInfos(tagCharType, text, mainGenericCode, secondNumber, null);
			GridInfos b = new GridInfos(tagCharType2, text2, mainGenericCode2, secondNumber2, null);
			if ((tagCharType == _tagCharType.CHARTYPE_NUMERAL_MAIN || tagCharType == _tagCharType.CHARTYPE_NUMERAL_SECOND || tagCharType == _tagCharType.CHARTYPE_NUMERAL_REVERSE_SECOND) && (tagCharType2 == _tagCharType.CHARTYPE_NUMERAL_MAIN || tagCharType2 == _tagCharType.CHARTYPE_NUMERAL_SECOND || tagCharType2 == _tagCharType.CHARTYPE_NUMERAL_REVERSE_SECOND))
			{
				res = GridInfos.GridInfoCompare(a, b);
				return true;
			}
			if ((tagCharType == _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN || tagCharType == _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND || tagCharType == _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND) && (tagCharType2 == _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN || tagCharType2 == _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND || tagCharType2 == _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND))
			{
				res = GridInfos.GridInfoCompare(a, b);
				return true;
			}
			return false;
		}

		public static bool IsValidatingOfName(string name, bool hasContainsPrefix)
		{
			string text = "";
			return AxisNameManager.IsValidatingOfName(name, hasContainsPrefix, ref text);
		}

		public static bool IsValidatingOfName(string name, bool hasContainsPrefix, ref string message)
		{
			string text = name;
			if (hasContainsPrefix)
			{
				string text2 = "";
				if (!AxisNameManager.IsContainsPrefix(name, ref text2, ref text))
				{
					text = name;
				}
			}
			if (text.IndexOf("I") != -1 || text.IndexOf("i") != -1 || text.IndexOf("O") != -1 || text.IndexOf("o") != -1 || text.IndexOf("Z") != -1 || text.IndexOf("z") != -1)
			{
				message = "轴号不符合标准格式！不能包含：I、i、O、o、Z、z";
				return false;
			}
			string text3 = "";
			int num = 0;
			int num2 = 0;
			if (AxisNameManager.CharType(name, ref text3, ref num, ref num2) == _tagCharType.CHARTYPE_OTHER)
			{
				message = "轴号不符合标准格式！\r\n正确格式：【分区名称】【分隔符】【辅轴号】/【主轴号】；\r\n注：1）‘分区’和‘辅轴’为可选项；\r\n       2）‘分隔符’可以为‘-’、‘_’和‘.’；\r\n示例：A、AA、1、12、1_1、A-1、1/A、1/1、A-1/A";
				return false;
			}
			return true;
		}

		public static List<string> GetSortSameKindGridLableList(string name, Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			name = name.ToUpper();
			string containsPrefix = "";
			int num = 0;
			int num2 = 0;
			_tagCharType tagCharType = AxisNameManager.CharType(name, ref containsPrefix, ref num, ref num2);
			if (tagCharType == _tagCharType.CHARTYPE_OTHER)
			{
				return list;
			}
			foreach (GridInfos gridInfos in AxisNameManager.GetSortSameKindGridInfosList(tagCharType, containsPrefix, doc))
			{
				list.Add(gridInfos.Name());
			}
			return list;
		}

		private static ICollection<Element> GetAllAxis(Autodesk.Revit.DB.Document doc)
		{
			ICollection<Element> collection = null;
			collection = new FilteredElementCollector(doc).OfClass(typeof(MultiSegmentGrid)).ToElements();
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in collection)
			{
				MultiSegmentGrid multiSegmentGrid = element as MultiSegmentGrid;
				if (multiSegmentGrid != null)
				{
					foreach (ElementId item in multiSegmentGrid.GetGridIds())
					{
						list.Add(item);
					}
				}
			}
			IEnumerator<Element> enumerator = new FilteredElementCollector(doc).OfClass(typeof(Grid)).ToElements().GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					Element ele = enumerator.Current;
					if (list.FindIndex((ElementId s) => s.Compare(ele.Id) == 0) < 0)
					{
						collection.Add(ele);
					}
				}
			}
			return collection;
		}

		public static List<string> GetAllAxisLables(Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			foreach (Element element in AxisNameManager.GetAllAxis(doc))
			{
				if (element.GetType() == typeof(MultiSegmentGrid))
				{
					MultiSegmentGrid multiSegmentGrid = element as MultiSegmentGrid;
					list.Add(multiSegmentGrid.Text);
				}
				else
				{
					string item = (element as Grid).get_Parameter(BuiltInParameter.DATUM_TEXT).AsString();
					list.Add(item);
				}
			}
			return list;
		}

		public static bool NumberIsAlreadyExisting(Autodesk.Revit.DB.Document doc, string lable)
		{
			return AxisNameManager.GetAllAxisLables(doc).IndexOf(lable) >= 0;
		}

		public static string GetLargestAlpha(Autodesk.Revit.DB.Document doc)
		{
			return AxisNameManager.GetLargestAlpha(AxisNameManager.GetAllAxisLables(doc));
		}

		public static int GetLargestNumber(Autodesk.Revit.DB.Document doc)
		{
			return AxisNameManager.GetLargestNumber(AxisNameManager.GetAllAxisLables(doc));
		}

		public static List<string> GetConnectorList()
		{
			return new List<string>
			{
				"-",
				"_",
				"."
			};
		}

		public static string GetContainsPrefix(string srcLable)
		{
			string result = "";
			string text = "";
			if (AxisNameManager.IsContainsPrefix(srcLable, ref result, ref text))
			{
				return result;
			}
			return "";
		}

		public static string GetRealLable(string srcLable)
		{
			string text = "";
			string result = srcLable;
			AxisNameManager.IsContainsPrefix(srcLable, ref text, ref result);
			return result;
		}

		private static string GetLargestAlpha(List<string> arLables)
		{
			List<GridInfos> list = new List<GridInfos>();
			foreach (string text in arLables)
			{
				string text2 = "";
				string text3 = text;
				if (!AxisNameManager.IsContainsPrefix(text, ref text2, ref text3))
				{
					string text4 = "";
					text3 = text3.ToUpper();
					int mainGenericCode = 0;
					int secondNumber = 0;
					_tagCharType tagCharType = AxisNameManager.CharType(text3, ref text4, ref mainGenericCode, ref secondNumber);
					if (tagCharType == _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN)
					{
						GridInfos item = new GridInfos(tagCharType, "", mainGenericCode, secondNumber, null);
						list.Add(item);
					}
				}
			}
			if (list.Count == 0)
			{
				return "A";
			}
			list.Sort(new Comparison<GridInfos>(GridInfos.GridInfoCompare));
			return list[list.Count - 1].GetNextGridInfos().Name();
		}

		private static int GetLargestNumber(List<string> arLables)
		{
			List<int> list = new List<int>();
			foreach (string text in arLables)
			{
				string text2 = "";
				string text3 = text;
				if (!AxisNameManager.IsContainsPrefix(text, ref text2, ref text3))
				{
					text3 = text3.ToUpper();
					int item = 0;
					int num = 0;
					string text4 = "";
					if (AxisNameManager.CharType(text3, ref text4, ref item, ref num) == _tagCharType.CHARTYPE_NUMERAL_MAIN)
					{
						list.Add(item);
					}
				}
			}
			if (list.Count == 0)
			{
				return 1;
			}
			list.Sort();
			return list[list.Count - 1] + 1;
		}

		private static List<GridInfos> GetSortSameKindGridInfosList(_tagCharType charType, string containsPrefix, Autodesk.Revit.DB.Document doc)
		{
			_tagCharType currentCharType = _tagCharType.CHARTYPE_OTHER;
			if (charType == _tagCharType.CHARTYPE_NUMERAL_MAIN || charType == _tagCharType.CHARTYPE_NUMERAL_SECOND || charType == _tagCharType.CHARTYPE_NUMERAL_REVERSE_SECOND)
			{
				currentCharType = (_tagCharType)7;
			}
			else if (charType == _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN || charType == _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND || charType == _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND)
			{
				currentCharType = (_tagCharType)56;
			}
			List<GridInfos> gridInfosList = AxisNameManager.GetGridInfosList(currentCharType, containsPrefix, doc);
			gridInfosList.Sort(new Comparison<GridInfos>(GridInfos.GridInfoCompare));
			return gridInfosList;
		}

		private bool FindGridInfos(string name, List<GridInfos> gridInfosList, ref int findIndex)
		{
			int i = 0;
			findIndex = -1;
			while (i < gridInfosList.Count)
			{
				if (gridInfosList[i].Name().CompareTo(name) == 0)
				{
					findIndex = i;
					break;
				}
				i++;
			}
			return -1 != findIndex;
		}

		private bool MainAuxiliaryConversion(string name, _tagCharType charType, int mainGenericCode, int secondNumber, ref string messgae)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			string containsPrefix = AxisNameManager.GetContainsPrefix(name);
			List<GridInfos> sortSameKindGridInfosList = AxisNameManager.GetSortSameKindGridInfosList(charType, containsPrefix, document);
			int currentIndex = -1;
			if (!this.FindGridInfos(name, sortSameKindGridInfosList, ref currentIndex))
			{
				return false;
			}
			if (charType == _tagCharType.CHARTYPE_NUMERAL_MAIN || charType == _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN)
			{
				this.MainToSecond(charType, currentIndex, ref sortSameKindGridInfosList);
			}
			else if (charType == _tagCharType.CHARTYPE_NUMERAL_SECOND || charType == _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND)
			{
				this.SecondToMain(charType, currentIndex, ref sortSameKindGridInfosList);
			}
			else
			{
				this.ReverseSecondToMain(charType, currentIndex, ref sortSameKindGridInfosList);
			}
			this.ChangeGridName(sortSameKindGridInfosList);
			return true;
		}

		private bool AddOneNewAxisName(string name, _tagCharType charType)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			string containsPrefix = AxisNameManager.GetContainsPrefix(name);
			List<GridInfos> sortSameKindGridInfosList = AxisNameManager.GetSortSameKindGridInfosList(charType, containsPrefix, document);
			int num = -1;
			if (!this.FindGridInfos(name, sortSameKindGridInfosList, ref num))
			{
				return false;
			}
			for (int i = num; i < sortSameKindGridInfosList.Count; i++)
			{
				GridInfos gridInfos = sortSameKindGridInfosList[i];
				if (i == sortSameKindGridInfosList.Count - 1)
				{
					if (gridInfos.IsMain)
					{
						gridInfos.MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeAddition(gridInfos.MainGenericCode, 1);
					}
					else
					{
						gridInfos.SecondNumber++;
					}
				}
				else
				{
					GridInfos gridInfos2 = sortSameKindGridInfosList[i + 1];
					gridInfos.Set(gridInfos2.TheGridType, containsPrefix, gridInfos2.MainGenericCode, gridInfos2.SecondNumber);
				}
			}
			this.ChangeGridName(sortSameKindGridInfosList);
			return true;
		}

		private bool ChangeName(string oldName, string newName, _tagCharType charType)
		{
			if (oldName.CompareTo(newName) == 0)
			{
				return false;
			}
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			string containsPrefix = AxisNameManager.GetContainsPrefix(oldName);
			string containsPrefix2 = AxisNameManager.GetContainsPrefix(newName);
			List<GridInfos> sortSameKindGridInfosList = AxisNameManager.GetSortSameKindGridInfosList(charType, containsPrefix, document);
			int num = -1;
			this.FindGridInfos(oldName, sortSameKindGridInfosList, ref num);
			int num2 = -1;
			this.FindGridInfos(newName, sortSameKindGridInfosList, ref num2);
			if (num == -1 && num2 == -1)
			{
				Element element = AxisNameManager.FindNameGetGrid(oldName, document);
				Element element2 = AxisNameManager.FindNameGetGrid(newName, document);
				if (element != null)
				{
					new GridInfos(_tagCharType.CHARTYPE_OTHER, containsPrefix, 0, 0, element).SetName(newName, document);
				}
				if (element2 != null)
				{
					new GridInfos(_tagCharType.CHARTYPE_OTHER, containsPrefix2, 0, 0, element2).SetName(oldName, document);
				}
			}
			else if (num2 == -1)
			{
				Element element3 = AxisNameManager.FindNameGetGrid(newName, document);
				if (element3 != null)
				{
					new GridInfos(_tagCharType.CHARTYPE_OTHER, containsPrefix2, 0, 0, element3).SetName(oldName, document);
				}
				sortSameKindGridInfosList[num].SetName(newName, document);
			}
			else if (num == -1)
			{
				Element element4 = AxisNameManager.FindNameGetGrid(oldName, document);
				if (element4 != null)
				{
					new GridInfos(_tagCharType.CHARTYPE_OTHER, containsPrefix, 0, 0, element4).SetName(newName, document);
				}
				sortSameKindGridInfosList[num2].SetName(oldName, document);
			}
			else
			{
				GridInfos gridInfos = sortSameKindGridInfosList[num];
				GridInfos gridInfos2 = sortSameKindGridInfosList[num2];
				_tagCharType theGridType = gridInfos.TheGridType;
				int mainGenericCode = gridInfos.MainGenericCode;
				int secondNumber = gridInfos.SecondNumber;
				_tagCharType theGridType2 = gridInfos2.TheGridType;
				int mainGenericCode2 = gridInfos2.MainGenericCode;
				int secondNumber2 = gridInfos2.SecondNumber;
				gridInfos.Set(theGridType2, containsPrefix2, mainGenericCode2, secondNumber2);
				gridInfos2.Set(theGridType, containsPrefix, mainGenericCode, secondNumber);
				this.ChangeGridName(sortSameKindGridInfosList);
			}
			return true;
		}

		private bool RemoveOneAxixName(string name, _tagCharType charType)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			string containsPrefix = AxisNameManager.GetContainsPrefix(name);
			List<GridInfos> sortSameKindGridInfosList = AxisNameManager.GetSortSameKindGridInfosList(charType, containsPrefix, document);
			int num = -1;
			if (!this.FindGridInfos(name, sortSameKindGridInfosList, ref num))
			{
				return false;
			}
			_tagCharType theGridType = sortSameKindGridInfosList[sortSameKindGridInfosList.Count - 1].TheGridType;
			int mainGenericCode = sortSameKindGridInfosList[sortSameKindGridInfosList.Count - 1].MainGenericCode;
			int secondNumber = sortSameKindGridInfosList[sortSameKindGridInfosList.Count - 1].SecondNumber;
			for (int i = sortSameKindGridInfosList.Count - 1; i >= num; i--)
			{
				GridInfos gridInfos = sortSameKindGridInfosList[i];
				if (i == num)
				{
					gridInfos.Set(theGridType, containsPrefix, mainGenericCode, secondNumber);
				}
				else
				{
					GridInfos gridInfos2 = sortSameKindGridInfosList[i - 1];
					gridInfos.Set(gridInfos2.TheGridType, containsPrefix, gridInfos2.MainGenericCode, gridInfos2.SecondNumber);
				}
			}
			this.ChangeGridName(sortSameKindGridInfosList);
			return true;
		}

		private void MainToSecond(_tagCharType charType, int currentIndex, ref List<GridInfos> gridInfosList)
		{
			GridInfos gridInfos = gridInfosList[currentIndex];
			if (currentIndex == 0)
			{
				gridInfos.SecondNumber = 1;
				gridInfos.TheGridType = this.MainAuxiliaryConversionWithType(gridInfos.TheGridType, true);
				for (int i = 0; i < currentIndex; i++)
				{
					gridInfosList[i].SecondNumber = gridInfosList[i].SecondNumber + 1;
				}
				int nextSameMainNumberGridInfosCount = this.GetNextSameMainNumberGridInfosCount(currentIndex, gridInfosList);
				if (nextSameMainNumberGridInfosCount > 0)
				{
					for (int i = currentIndex + 1; i <= currentIndex + nextSameMainNumberGridInfosCount; i++)
					{
						if (i == currentIndex + 1)
						{
							gridInfosList[i].TheGridType = this.MainAuxiliaryConversionWithType(gridInfosList[i].TheGridType, false);
						}
						else
						{
							gridInfosList[i].SecondNumber--;
						}
					}
					return;
				}
				for (int i = currentIndex + 1; i < gridInfosList.Count; i++)
				{
					gridInfosList[i].MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeSubtraction(gridInfosList[i].MainGenericCode, 1);
				}
				return;
			}
			else
			{
				int nextSameMainNumberGridInfosCount2 = this.GetNextSameMainNumberGridInfosCount(currentIndex, gridInfosList);
				gridInfos.MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeSubtraction(gridInfos.MainGenericCode, 1);
				gridInfos.TheGridType = this.MainAuxiliaryConversionWithType(gridInfos.TheGridType, false);
				int num = this.GetPreviousSameMainNumberGridInfosCount(currentIndex, gridInfosList);
				if (num == 0)
				{
					num = (gridInfos.SecondNumber = num + 1);
				}
				else
				{
					gridInfos.SecondNumber = num;
				}
				if (nextSameMainNumberGridInfosCount2 > 0)
				{
					for (int j = currentIndex + 1; j <= currentIndex + nextSameMainNumberGridInfosCount2; j++)
					{
						num = (gridInfosList[j].SecondNumber = num + 1);
						gridInfosList[j].MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeSubtraction(gridInfosList[j].MainGenericCode, 1);
					}
					for (int j = currentIndex + nextSameMainNumberGridInfosCount2 + 1; j < gridInfosList.Count; j++)
					{
						gridInfosList[j].MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeSubtraction(gridInfosList[j].MainGenericCode, 1);
					}
					return;
				}
				for (int j = currentIndex + 1; j < gridInfosList.Count; j++)
				{
					gridInfosList[j].MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeSubtraction(gridInfosList[j].MainGenericCode, 1);
				}
				return;
			}
		}

		private void SecondToMain(_tagCharType charType, int currentIndex, ref List<GridInfos> gridInfosList)
		{
			GridInfos gridInfos = gridInfosList[currentIndex];
			int nextSameMainNumberGridInfosCount = this.GetNextSameMainNumberGridInfosCount(currentIndex, gridInfosList);
			if (nextSameMainNumberGridInfosCount > 0)
			{
				int num = 1;
				int i = currentIndex + 1;
				while (i <= currentIndex + nextSameMainNumberGridInfosCount)
				{
					gridInfosList[i].SecondNumber = num;
					gridInfosList[i].MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeAddition(gridInfosList[i].MainGenericCode, 1);
					i++;
					num++;
				}
				while (i < gridInfosList.Count)
				{
					gridInfosList[i].MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeAddition(gridInfosList[i].MainGenericCode, 1);
					i++;
				}
			}
			else
			{
				for (int i = currentIndex + 1; i < gridInfosList.Count; i++)
				{
					gridInfosList[i].MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeAddition(gridInfosList[i].MainGenericCode, 1);
				}
			}
			gridInfos.MainGenericCode = CharStringAndGenericCodeConvert.GeneticCodeAddition(gridInfos.MainGenericCode, 1);
			gridInfos.TheGridType = this.MainAuxiliaryConversionWithType(gridInfos.TheGridType, false);
		}

		private void ReverseSecondToMain(_tagCharType charType, int currentIndex, ref List<GridInfos> gridInfosList)
		{
			GridInfos gridInfos = gridInfosList[currentIndex];
			int i = currentIndex - 1;
			int num = 1;
			while (i >= 0)
			{
				gridInfosList[i].SecondNumber = num;
				i--;
				num++;
			}
			int nextSameMainNumberGridInfosCount = this.GetNextSameMainNumberGridInfosCount(currentIndex, gridInfosList);
			if (nextSameMainNumberGridInfosCount > 0)
			{
				_tagCharType theGridType = _tagCharType.CHARTYPE_NUMERAL_SECOND;
				if (gridInfos.TheGridType == _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND)
				{
					theGridType = _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND;
				}
				num = 1;
				i = currentIndex + 1;
				while (i <= currentIndex + nextSameMainNumberGridInfosCount)
				{
					gridInfosList[i].SecondNumber = num;
					gridInfosList[i].TheGridType = theGridType;
					i++;
					num++;
				}
			}
			gridInfos.MainGenericCode = 1;
			gridInfos.TheGridType = this.MainAuxiliaryConversionWithType(gridInfos.TheGridType, false);
		}

		private void ChangeGridName(List<GridInfos> gridInfosList)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			for (int i = 0; i < gridInfosList.Count; i++)
			{
				GridInfos gridInfos = gridInfosList[i];
				string name = "temp" + i.ToString();
				gridInfos.SetName(name, document);
			}
			foreach (GridInfos gridInfos2 in gridInfosList)
			{
				gridInfos2.SetName(gridInfos2.Name(), document);
			}
		}

		private int GetPreviousSameMainNumberGridInfosCount(int currentIndex, List<GridInfos> gridInfosList)
		{
			GridInfos gridInfos = gridInfosList[currentIndex];
			int num = 0;
			for (int i = currentIndex - 1; i >= 0; i--)
			{
				if (gridInfosList[i].TheGridType != _tagCharType.CHARTYPE_NUMERAL_REVERSE_SECOND && gridInfosList[i].TheGridType != _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND)
				{
					if (gridInfosList[i].MainGenericCode != gridInfos.MainGenericCode)
					{
						break;
					}
					num++;
				}
			}
			return num;
		}

		private int GetNextSameMainNumberGridInfosCount(int currentIndex, List<GridInfos> gridInfosList)
		{
			GridInfos gridInfos = gridInfosList[currentIndex];
			int num = 0;
			int num2 = currentIndex + 1;
			while (num2 < gridInfosList.Count && gridInfosList[num2].MainGenericCode == gridInfos.MainGenericCode)
			{
				num++;
				num2++;
			}
			return num;
		}

		private _tagCharType MainAuxiliaryConversionWithType(_tagCharType charType, bool toReverse)
		{
			_tagCharType result;
			if (charType == _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN)
			{
				if (toReverse)
				{
					result = _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND;
				}
				else
				{
					result = _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND;
				}
			}
			else if (charType == _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND)
			{
				result = _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN;
			}
			else if (charType == _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND)
			{
				result = _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN;
			}
			else if (charType == _tagCharType.CHARTYPE_NUMERAL_MAIN)
			{
				if (toReverse)
				{
					result = _tagCharType.CHARTYPE_NUMERAL_REVERSE_SECOND;
				}
				else
				{
					result = _tagCharType.CHARTYPE_NUMERAL_SECOND;
				}
			}
			else if (charType == _tagCharType.CHARTYPE_NUMERAL_SECOND)
			{
				result = _tagCharType.CHARTYPE_NUMERAL_MAIN;
			}
			else
			{
				result = _tagCharType.CHARTYPE_NUMERAL_MAIN;
			}
			return result;
		}

		private static List<GridInfos> GetGridInfosList(_tagCharType currentCharType, string containsPrefix, Autodesk.Revit.DB.Document doc)
		{
			List<GridInfos> list = new List<GridInfos>();
			foreach (Element element in AxisNameManager.GetAllAxis(doc))
			{
				string name = element.Name;
				string text = "";
				int mainGenericCode = 0;
				int secondNumber = 0;
				_tagCharType tagCharType = AxisNameManager.CharType(name, ref text, ref mainGenericCode, ref secondNumber);
				if (containsPrefix.CompareTo(text) == 0 && (tagCharType & currentCharType) != _tagCharType.CHARTYPE_OTHER)
				{
					GridInfos item = new GridInfos(tagCharType, text, mainGenericCode, secondNumber, element);
					list.Add(item);
				}
			}
			return list;
		}

		private static _tagCharType CharType(string strChar, ref string containsPrefix, ref int mainGenericCode, ref int secondNumber)
		{
			string text = "";
			string text2 = "";
			if (AxisNameManager.IsContainsPrefix(strChar, ref text, ref text2))
			{
				strChar = text2;
				containsPrefix = text;
			}
			_tagCharType tagCharType = _tagCharType.CHARTYPE_OTHER;
			int num = strChar.IndexOf("/");
			if (num != -1)
			{
				string strChar2 = strChar.Substring(0, num);
				int num2 = 0;
				int num3 = 0;
				string text3 = "";
				if (AxisNameManager.CharType(strChar2, ref text3, ref num2, ref num3) == _tagCharType.CHARTYPE_NUMERAL_MAIN)
				{
					string text4 = strChar.Substring(num + 1, strChar.Length - num - 1);
					if (text4.CompareTo("01") == 0)
					{
						mainGenericCode = 1;
						secondNumber = num2;
						return _tagCharType.CHARTYPE_NUMERAL_REVERSE_SECOND;
					}
					if (text4.CompareTo("0A") == 0)
					{
						mainGenericCode = CharStringAndGenericCodeConvert.EnglishToGenericCode("A");
						secondNumber = num2;
						return _tagCharType.CHARTYPE_ENGLISHCHAR_REVERSE_SECOND;
					}
					int num4 = 0;
					string text5 = "";
					_tagCharType tagCharType2 = AxisNameManager.CharType(text4, ref text5, ref num4, ref num3);
					if (tagCharType2 == _tagCharType.CHARTYPE_NUMERAL_MAIN)
					{
						mainGenericCode = num4;
						secondNumber = num2;
						return _tagCharType.CHARTYPE_NUMERAL_SECOND;
					}
					if (tagCharType2 == _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN)
					{
						mainGenericCode = num4;
						secondNumber = num2;
						return _tagCharType.CHARTYPE_ENGLISHCHAR_SECOND;
					}
				}
			}
			for (int i = 0; i < strChar.Length; i++)
			{
				int num5 = CharAndNumberConvert.Chr2AscII(new string(strChar[i], 1));
				_tagCharType tagCharType3;
				if (num5 >= 65 && num5 <= 90)
				{
					tagCharType3 = _tagCharType.CHARTYPE_ENGLISHCHAR_MAIN;
				}
				else
				{
					if (num5 < 48 || num5 > 57)
					{
						return _tagCharType.CHARTYPE_OTHER;
					}
					tagCharType3 = _tagCharType.CHARTYPE_NUMERAL_MAIN;
				}
				if (i == 0)
				{
					tagCharType = tagCharType3;
				}
				else if (tagCharType != tagCharType3)
				{
					mainGenericCode = 0;
					secondNumber = 0;
					return _tagCharType.CHARTYPE_OTHER;
				}
			}
			if (tagCharType == _tagCharType.CHARTYPE_NUMERAL_MAIN)
			{
				mainGenericCode = Convert.ToInt32(strChar);
			}
			else
			{
				mainGenericCode = CharStringAndGenericCodeConvert.EnglishToGenericCode(strChar);
			}
			secondNumber = 0;
			return tagCharType;
		}

		private static bool IsContainsPrefix(string srcLable, ref string preLable, ref string realLable)
		{
			foreach (string value in AxisNameManager.GetConnectorList())
			{
				if (srcLable.Contains(value))
				{
					int num = srcLable.IndexOf(value);
					preLable = srcLable.Substring(0, num + 1);
					realLable = srcLable.Substring(num + 1);
					return true;
				}
			}
			return false;
		}

		private ExternalCommandData m_Revit;
	}
}
